package sqlite.com.sqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ImageView;
import android.widget.ListView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;

// https://blog.csdn.net/bzlj2912009596/article/details/80331812        参考文档
//   https://www.cnblogs.com/endv/p/9774417.html                       参考文档
public class MainActivity extends AppCompatActivity {
    public static ArrayList<Music> musicList=new ArrayList<>();
    public static Music music;
    public static ListView listView;
    public static ArrayList<byte[]> img_res=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.list_view);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("https://api.androidhive.info/music/music.xml");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    if (conn.getResponseCode() != 200)
                        throw new RuntimeException("请求url失败");

                    InputStream is = conn.getInputStream();// 获取返回数据

                    // 使用输出流来输出字符(可选)
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = is.read(buf)) != -1) {
                        out.write(buf, 0, len);
                    }
                    String string = out.toString("UTF-8");
                    //                    System.out.println("返回的数据"+string);
                    Log.i("返回的数据", string);
                    Log.i("看字符窜第一个字符1", string);
                    out.close();
                    XmlPullParser parser = Xml.newPullParser();
                    Log.i("看字符窜第一个字符2", string);
                    parser.setInput(new ByteArrayInputStream(string.getBytes("UTF-8")), "UTF-8");
                    //                    parser.setInput(inputStream,"UTF-8");

                    int eventType = parser.getEventType();
                    Log.i("开始了", string);

                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        eventType = parser.next();
                        switch (eventType) {
                            case XmlPullParser.START_DOCUMENT:
                                Log.i("START_DOCUMENT", "START_DOCUMENT");
                                break;
                            case XmlPullParser.START_TAG:
                                Log.i("在循环里", "在循环里");
                                if (eventType == XmlPullParser.START_TAG) {
                                    music=new Music();
                                    Log.i("if里面", "if里面");
                                    if ("id".equals(parser.getName())) {
                                        Log.i("第一个if里面", "第一个if里面");
                                        String id = parser.nextText();
                                        music.setId(Integer.valueOf(id));
                                        Log.i("数据——id", id);
                                    } else if ("title".equals(parser.getName())) {
                                        String title = parser.nextText();
                                        music.setTitle(title);
                                        Log.i("数据——title", title);
                                    } else if ("artist".equals(parser.getName())) {
                                        String artist = parser.nextText();
                                        music.setArtist(artist);
                                        Log.i("数据——artist", artist);
                                    } else if ("duration".equals(parser.getName())) {
                                        String duration = parser.nextText();
                                        music.setDuration(duration);
                                        Log.i("数据——duration", duration);
                                    } else if ("thumb_url".equals(parser.getName())) {
                                        String thumb_url = parser.nextText();
                                        music.setThumb_url(thumb_url);
                                        Log.i("数据——thumb_url", thumb_url);
                                        URL imageURL=new URL(thumb_url);
                                        HttpURLConnection img_connection = (HttpURLConnection) imageURL.openConnection();
                                        InputStream img_is = img_connection.getInputStream();

                                        ByteArrayOutputStream img_byteArrayOut = new ByteArrayOutputStream();
                                        byte[] img_buffer = new byte[1024];
                                        int img_len;
                                        while ((img_len = img_is.read(img_buffer)) != -1) {
                                            img_byteArrayOut.write(img_buffer, 0, img_len);
                                        }
                                        MainActivity.img_res.add(img_byteArrayOut.toByteArray());
                                    }
                                }
                                musicList.add(music);
                                break;
                            case XmlPullParser.END_TAG:

                                break;
                        }

//                        eventType = parser.next();
                    }



                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MainActivity.listView.setAdapter(new MyBaseAdapter(MainActivity.this,musicList,MainActivity.img_res));
                        }
                    });


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
